package com.zzsin.gtestreporter.report;

import java.io.File;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class GTestReport {

    private List<TestTask> ttList = new LinkedList<TestTask>();
    private String path;

    public GTestReport(String path){
        this.path = path;
    }

    public int parse(){
        File dirFile = new File(this.path);
        if(!dirFile.exists()){
            return -1;
        }
        if(dirFile.isFile()){
            return -2;
        }

        for(File f : dirFile.listFiles()){
            long time = f.lastModified();
            // System.out.println("time: " + time);
            if(f.isFile()){
                continue;
            }
            TestTask tt = new TestTask(f.getAbsolutePath(), f.getName(), time);
            tt.parse();
            ttList.add(tt);
        }

        ttList.sort(new Comparator<TestTask>() {
            @Override
            public int compare(TestTask o1, TestTask o2) {
                if(o1.getTime() > o2.getTime()){
                    return -1;
                }
                if(o1.getTime() < o2.getTime()){
                    return 1;
                }
                if(o1.getTime() == o2.getTime()){
                    return 0;
                }
                return 0;
            }
        });

        return 0;
    }

    public TestTask getTestTask(String id){
        TestTask tt = null;
        for(TestTask t : ttList){
            if(t.getId().equals(id)){
                tt = t;
            }
        }

        // System.out.println("x:" + tt.getId());

        return tt;
    }

    public List<TestTask> getTtList() {
        return ttList;
    }

    public void setTtList(List<TestTask> ttList) {
        this.ttList = ttList;
    }
}
